package com.vf.admin.platform.controller;

import com.vf.admin.common.BaseController;
import com.vf.admin.common.Constants;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.platform.entity.Api;
import com.vf.admin.platform.service.ApiService;
import com.vf.admin.utils.StringEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 客户端API管理
 */
@Controller
@RequestMapping(value = "/api")
public class ApiController extends BaseController{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ApiService apiService;

    /**
     * 获取列表
     * @return
     */
    @RequestMapping("/list")
    public String list(SplitPageBean pageBean, Api api) {
        try {
            Map paraMap = new HashMap();
            if(api!=null){
                paraMap.put("apiname",api.getApiname());
                paraMap.put("apitype",api.getApitype());
                paraMap.put("loginstate",api.getLoginstate());
            }
            SplitPageBean splitPage = new SplitPageBean(pageBean.getPageIndex(),pageBean.getPageSize());
            List<Api> list = apiService.findList(paraMap, splitPage);

            request.setAttribute("api",api);
            request.setAttribute("list",list);
            request.setAttribute("splitPage",splitPage);
            request.setAttribute("apiStateMap",Constants.getApiStateMap());
            request.setAttribute("apitype",Constants.getApiTypeMap());
            return this.sucess("/web/platform/api/list");
        } catch (Exception e) {
            logger.error("查询订单列表异常",e);
            return this.error("操作异常，请联系管理员");
        }
    }

    /**
     * 转向编辑页面
     * @return
     */
    @RequestMapping("/detail")
    public String detail(Integer id){
        try{
            if (id!=null && id>0) {
                Api api = apiService.get(id);
                request.setAttribute("api",api);
            }
            request.setAttribute("apiStateMap",Constants.getApiStateMap());
            request.setAttribute("apitype",Constants.getApiTypeMap());
            return this.sucess("/web/platform/api/edit");
        }catch(Exception e){
            logger.error("查询详情失败", e);
            return this.error("查询详情失败");
        }
    }

    /**
     *保存或更新
     */
    @RequestMapping("/save")
    @ResponseBody
    public Result addOrUpdate(Api api){
        try {
            if(StringEx.isNull(api.getApiname())){
                return this.errorJSON("接口名不能为空");
            }
            if(StringEx.isNull(api.getApitype())){
                return this.errorJSON("接口类型不能为空");
            }

            if(StringEx.isNull(api.getApiurl())){
                return this.errorJSON("接口地址不能为空");
            }
            if(StringEx.isNullOrLing(api.getId())){
                apiService.save(api);
            }else{
                Api clientBT = apiService.get(api.getId());
                clientBT.setApiname(api.getApiname());
                clientBT.setApitype(api.getApitype());
                clientBT.setApiurl(api.getApiurl());
                clientBT.setLoginstate(api.getLoginstate() == null ? Constants.API_BDL : api.getLoginstate());
                clientBT.setMessage(api.getMessage());
                apiService.save(clientBT);
            }
            Constants.ClientApiCacheMap.clear();
            return this.sucessJSON("保存成功");
        }catch (Exception e){
            logger.error("保存客户端接口信息异常",e);
            return this.errorJSON("操作异常，请联系管理员");
        }
    }

    /**
     * 删除
     * @param idArray
     * @return
     */
    @RequestMapping("/del")
    @ResponseBody
    public Result del(String idArray){
        try{
            if(StringEx.isNull(idArray)){
                return this.errorJSON("接口权限ID不能为空!");
            }
            String[] userIdArray = idArray.split(",");
            if(userIdArray!=null && userIdArray.length>0) {
                for (String id : userIdArray) {
                    if (StringEx.isNull(id)) {
                        continue;
                    }
                    Api api = apiService.get(Integer.valueOf(id));
                    if (api != null && api.getId()!=null && api.getId() > 0) {
                        apiService.del(api.getId());
                    }
                }
            }

            Constants.ClientApiCacheMap.clear();
            return this.sucess();
        }catch (Exception e){
            logger.error("删除客户端接口信息异常", e);
            return this.errorJSON("操作异常，请联系管理员");
        }
    }
}
